unit uMainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  GlScene, GlObjects, GlCadencer, GLWin32Viewer, GLRenderContextInfo, GlWindowsFont, GlHudObjects,
  GlMaterial,
  VectorGeometry,
  uTweener,
  uSimpleAnimationsObjects;
  
const
  MatNames: array [1..5] of String = ('loaderfront', 'loaderlines', 'loading', 'line_1', 'back');
  ZooNames: array [1..4] of String = ('Raccoon', 'Squirrel', 'Warthog', 'Zebra');
  ZooHints: array [1..4] of String = ('Raccoon_hint', 'Squirrel_hint', 'Warthog_hint', 'Zebra_hint');
type
  
  TfrmMain = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    {Main Objects}
    fGlScene: TGlScene;
    fCadencer: TGLCadencer;
    fViewer: TGLSceneViewer;
    fCamera: TGlCamera;
    fMainDummy: TGlDummyCube;
    fFpsTimer: Single;
    fMatLib: TGlMaterialLibrary;

    {Text Objects}
    fWinFont: TGLWindowsBitmapFont;
    fHudText: TGLHUDText;
    fBack: TGlHudSprite;

    {Loader objects}
    fLoaderBar: TLoaderBar;
    fLoadingHint: TSimpleImageHint;

    fWaitAnimation: TWaitAnimation;

    {Zoo objects}
    Zoo: array[1..4] of TGlHudSprite;
    ZooHint: array[1..4] of TSimpleImageHint;

    {Tween-objects}
    fTweener: TTweener;

    MousePressed: Boolean;
  public
    Procedure InitFontAndSprites;
    Procedure InitMaterials;
    Procedure InitTweener;
    Procedure OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
  end;

var
  frmMain: TfrmMain;

implementation
uses
  glKeyboard,
  GLCrossPlatform,
  gltexture,
  uDemoInit;
{$R *.dfm}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  fFpsTimer := 0;
  CreateDemo(self, fGlScene, fCadencer, fViewer);
  
  fCamera := TGlCamera.CreateAsChild(fGlScene.Objects);
  with fCamera do
  begin
    Position.SetPoint(0, 0, 5);
    Direction.SetVector(0, 0, -1);
    Up.SetVector(0, 1, 0);
    CameraStyle := csOrthogonal;
    SceneScale := 0.1;
  end;

  fMainDummy := TGLDummyCube.CreateAsChild(fGlScene.Objects);
  fMainDummy.ObjectsSorting := osNone;

  fViewer.Camera := fCamera;
  fCadencer.OnProgress := OnCadencerProgress;

  fMatLib := TGlMaterialLibrary.Create(self);

  InitMaterials;
  InitFontAndSprites;
  InitTweener;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  FreeAndNil(fTweener);
end;

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  fCadencer.Enabled := true;
end;

Procedure TfrmMain.InitFontAndSprites;
var
  i: integer;
begin
  fWinFont := TGLWindowsBitmapFont.Create(self);
  fWinFont.Font.Name := 'Arial';

  fBack := TGlHudSprite.CreateAsChild(fMainDummy);
  with fBack do
  begin
    Material.LibMaterialName := 'back';
    Material.MaterialLibrary := fMatLib;
    Width := Material.GetActualPrimaryTexture.Image.Width;
    Height := Material.GetActualPrimaryTexture.Image.Height;
    Position.SetPoint(Width / 2, Height / 2, 0);
  end;

  fHudText := TGLHUDText.CreateAsChild(fMainDummy);
  with fHudText do
  begin
    BitmapFont := fWinFont;
    Text := 'Lampogolovii presents ''SimpleAnimations Demo''';
    ModulateColor.SetColor(1, 1, 1, 0.7);
    Position.SetPoint(5, 2, 0);
    Alignment := taLeftJustify;
  end;

  fLoadingHint := TSimpleImageHint.CreateAsChild(fMainDummy);
  with fLoadingHint do
  begin
    Material.LibMaterialName := 'loading';
    Material.MaterialLibrary := fMatLib;
    Width := Material.GetActualPrimaryTexture.Image.Width;
    Height := Material.GetActualPrimaryTexture.Image.Height;
    SetPositions(VectorMake(300, 330, 0), VectorMake(300, 360, 0));
  end;

  fLoaderBar := TLoaderBar.CreateAsChild(fMainDummy);
  fLoaderBar.SetMaterials(fMatLib, 'loaderlines' , 'loaderfront');
  fLoaderBar.Position.SetPoint(300, 360, 0);

  fWaitAnimation := TWaitAnimation.CreateAsChild(fMainDummy);
  with fWaitAnimation do
  begin
    Material.LibMaterialName := 'line_1';
    Material.MaterialLibrary := fMatLib;
    Width := Material.GetActualPrimaryTexture.Image.Width;
    Height := Material.GetActualPrimaryTexture.Image.Height;
    Position.SetPoint(300, 250, 0);
  end;

  for i := 1 to 4 do
  begin
    ZooHint[i] := TSimpleImageHint.CreateAsChild(fMainDummy);
    with ZooHint[i] do
    begin
      Material.LibMaterialName := ZooNames[i] + '_hint';
      Material.MaterialLibrary := fMatLib;
      Width := Material.GetActualPrimaryTexture.Image.Width;
      Height := Material.GetActualPrimaryTexture.Image.Height;
      SetPositions(VectorMake(80 + (i - 1) * 150, 180, 0), VectorMake(80 + (i - 1) * 150, 120, 0));
    end;

    Zoo[i] := TGlHudSprite.CreateAsChild(fMainDummy);
    with Zoo[i] do
    begin
      Material.LibMaterialName := ZooNames[i];
      Material.MaterialLibrary := fMatLib;
      Width := Material.GetActualPrimaryTexture.Image.Width;
      Height := Material.GetActualPrimaryTexture.Image.Height;
      Position.SetPoint(80 + (i - 1) * 150, 100, 0);
    end;
  end;
end;

Procedure TfrmMain.InitMaterials;
begin
  AddMaterials(fMatLib, str_ImagesFolder + '\SimpleAnimations\', ['LoaderFront.png', 'LoaderLines_1.png', 'Loading_text.png', 'line_1.png', 'Back.png'], MatNames);
  AddMaterials(fMatLib, str_ImagesFolder + '\SimpleAnimations\', ['Raccoon.png', 'Squirrel.png', 'Warthog.png', 'Zebra.png'], ZooNames);
  AddMaterials(fMatLib, str_ImagesFolder + '\SimpleAnimations\', ['Raccoon_hint.png', 'Squirrel_hint.png', 'Warthog_hint.png', 'Zebra_hint.png'], ZooHints);
end;

Procedure TfrmMain.InitTweener;
begin
  MousePressed := false;
  fTweener := TTweener.Create;
end;

Procedure TfrmMain.OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
var
  MPos: TPoint;
  i: integer;
begin
  fTweener.Update(DeltaTime);

  GetCursorPos(Mpos);
  MPos := fViewer.ScreenToClient(MPos);

  MousePressed := IsKeyDown(vk_LButton);

  if fLoaderBar.IsHit(VectorMake(MPos.x, MPos.y, 0)) then
    fLoadingHint.Show
  else
    fLoadingHint.Hide;

  for i := 1 to 4 do
    with Zoo[i] do
      if  (MPos.x >= Position.X - Width / 2) and (MPos.y >= Position.Y - Height / 2)
      and (MPos.x <= Position.X + Width / 2) and (MPos.y <= Position.Y + Height / 2) then
        ZooHint[i].Show
      else
        ZooHint[i].Hide;
end;

end.

